package com.pianzhu.blockchainemr.controller;

import com.pianzhu.blockchainemr.pojo.MedicalRecords;
import com.pianzhu.blockchainemr.pojo.Patient;
import com.pianzhu.blockchainemr.pojo.result.MedicalRecordListResult;
import com.pianzhu.blockchainemr.pojo.result.RegistryRecordListResult;
import com.pianzhu.blockchainemr.pojo.result.Result;
import com.pianzhu.blockchainemr.service.PatientService;
import com.pianzhu.blockchainemr.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @GetMapping("/medicalRecord/{username}")
    public Result getMedicalRecord(@PathVariable String username) {
        if (!CurrentHolder.getCurrentLocal().equals(username)){
            return Result.error("token error");
        }
        List<MedicalRecordListResult> medicalRecords = patientService.getMedicalRecord(username);
        return Result.success(medicalRecords);
    }

    @GetMapping("/registryRecord/{username}")
    public Result getRegistryRecord(@PathVariable String username) {
        if (!CurrentHolder.getCurrentLocal().equals(username)){
            return Result.error("token error");
        }
        List<RegistryRecordListResult> registryRecords = patientService.getRegistryRecord(username);
        return Result.success(registryRecords);
    }

    @PostMapping("/registary")
    public Result register(@RequestBody MedicalRecords medicalRecords) {
        if (!CurrentHolder.getCurrentLocal().equals(medicalRecords.getUsername())){
            return Result.error("token error");
        }
        medicalRecords.setRegisterTime(LocalDateTime.now());
        patientService.register(medicalRecords);
        return Result.success();
    }

    @GetMapping("/profile/list/{username}")
    public Result getProfileList(@PathVariable String username) {
        if (!CurrentHolder.getCurrentLocal().equals(username)){
            return Result.error("token error");
        }
        List<MedicalRecords> medicalRecords = patientService.getProfileList(username);
        return Result.success(medicalRecords);
    }

    @GetMapping("/profile/sufferlist/{username}")
    public Result getSufferedList(@PathVariable String username) {
        if (!CurrentHolder.getCurrentLocal().equals(username)){
            return Result.error("token error");
        }
        List<String> suffer = patientService.getSufferList(username);
        return Result.success(suffer);
    }

}
